package com.at.abc.r421_e;

import java.io.*;
import java.util.*;

/**
 * 使用双向链表 + HashMap 实现题目要求。
 * 初始 A = [0]
 * 每次第 i 个查询:
 *  1 x  -> 在值为 x 的位置之后插入值 i
 *  2 x y-> 删除 x 和 y 之间的所有元素（不含 x,y），输出被删元素之和
 */
public class e {
    static class Node {
        int val;
        Node prev, next;
        Node(int v){ val = v; }
    }

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        int Q = Integer.parseInt(br.readLine().trim());
        StringBuilder sb = new StringBuilder();

        // 链表初始化：A = [0]
        Node head = new Node(0);
        // 头尾指针（头就是唯一节点）
        Node tail = head;

        // 值 -> 节点 映射，方便按值找到节点
        HashMap<Integer, Node> map = new HashMap<>();
        map.put(0, head);

        for (int i = 1; i <= Q; ++i) {
            String line = br.readLine();
            while (line != null && line.trim().isEmpty()) line = br.readLine();
            if (line == null) break;
            st = new StringTokenizer(line);

            int type = Integer.parseInt(st.nextToken());
            if (type == 1) {
                int x = Integer.parseInt(st.nextToken());
                Node nx = map.get(x);
                // 按题意保证 x 一定存在
                if (nx == null) {
                    throw new RuntimeException("value not found for insert: " + x);
                }

                Node newNode = new Node(i); // 插入的值是当前查询编号 i
                // 插入到 nx 之后
                Node nxt = nx.next;
                nx.next = newNode;
                newNode.prev = nx;
                newNode.next = nxt;
                if (nxt != null) nxt.prev = newNode;
                else tail = newNode; // 如果插在尾部，更新 tail
                map.put(newNode.val, newNode);
            } else if (type == 2) {
                int x = Integer.parseInt(st.nextToken());
                int y = Integer.parseInt(st.nextToken());
                if (x == y) {
                    // 同一位置，之间没有元素
                    sb.append("0\n");
                    continue;
                }
                Node nx = map.get(x);
                Node ny = map.get(y);
                if (nx == null || ny == null) {
                    throw new RuntimeException("value(s) not found for remove: " + x + " " + y);
                }

                // 先判断谁在前面（沿 next 向后找）
                boolean forward = false;
                Node cur = nx.next;
                while (cur != null) {
                    if (cur == ny) { forward = true; break; }
                    cur = cur.next;
                }

                long sum = 0L;
                if (forward) {
                    // nx 在 ny 之前：删除 nx.next ... ny.prev
                    cur = nx.next;
                    while (cur != ny) {
                        sum += cur.val;
                        map.remove(cur.val);
                        Node tmp = cur.next;
                        // 断开引用，帮助 GC
                        cur.prev = null;
                        cur.next = null;
                        cur = tmp;
                    }
                    // 连接 nx 和 ny
                    nx.next = ny;
                    ny.prev = nx;
                    if (ny == null) tail = nx; // （ny 不可能为 null，因为 found ny）
                } else {
                    // 否则，ny 在 nx 之前：删除 ny.next ... nx.prev
                    cur = ny.next;
                    while (cur != nx) {
                        sum += cur.val;
                        map.remove(cur.val);
                        Node tmp = cur.next;
                        cur.prev = null;
                        cur.next = null;
                        cur = tmp;
                    }
                    // 连接 ny 和 nx
                    ny.next = nx;
                    nx.prev = ny;
                    if (nx == null) tail = ny; // （nx 不可能为 null）
                }

                sb.append(sum).append('\n');
            } else {
                throw new RuntimeException("unknown query type: " + type);
            }
        }

        System.out.print(sb.toString());
    }
}
